package com.cn.ccp.action.onlineEdu;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.faces.context.FacesContext;

import org.apache.log4j.Logger;

import com.cn.ccp.action.BaseAction;
import com.cn.ccp.bean.CourseBean;
import com.cn.ccp.bean.SelectedCourseBean;
import com.cn.ccp.bean.StudentMarkedHistoryBean;

/**
 * 学生评价
 * @author renyongcheng
 *
 */
public class OnlineMarkAction extends BaseAction {
	private Logger log = Logger.getLogger(OnlineMarkAction.class);
	private CourseBean course;
	private String selectedCourseId,courseid;//选课id
	private String starLevel;//评价星级
	private String contents;//评价详细内容
	private List<StudentMarkedHistoryBean> markedList;
	private boolean hasMarked = true;
	public OnlineMarkAction(){
		log.info("---------OnlineMarkAction()---------");
		Map<String, String> varMap = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
		if(varMap.get("courseid") != null  && !varMap.get("courseid").equals("")){
			courseid = varMap.get("courseid");
			getpagedetail();
		}
		log.info("---courseid = "+courseid);
		try {
			if(varMap.get("settingId") != null  && !varMap.get("settingId").equals("")){
				selectedCourseId = getCourseService().getSelectCourseId(varMap.get("settingId"));
				Object scb = getSelectCourseService().getSelectedCourseBySelectedId(selectedCourseId);
				if(scb != null){
					SelectedCourseBean sc = (SelectedCourseBean)scb;
					hasMarked = !sc.isHasMarked();
				}
			}
			if(varMap.get("selectedCourseId") != null  && !varMap.get("selectedCourseId").equals("")){
				selectedCourseId = varMap.get("selectedCourseId") ;
				Object scb = getSelectCourseService().getSelectedCourseBySelectedId(selectedCourseId);
				if(scb != null){
					SelectedCourseBean sc = (SelectedCourseBean)scb;
					hasMarked = !sc.isHasMarked();
				}
			}
		} catch (Exception e) {
			
		}
		log.info("---selectedCourseId = "+selectedCourseId);
	}
	
	public String doMark(){
		log.info("---selectedCourseId = "+selectedCourseId);
		log.info("---courseid = "+courseid);
		getCourseService().doMarkCourse(selectedCourseId, Integer.parseInt(starLevel), contents, courseid);
		getpagedetail();
		Object scb = getSelectCourseService().getSelectedCourseBySelectedId(selectedCourseId);
		if(scb != null){
			SelectedCourseBean sc = (SelectedCourseBean)scb;
			hasMarked = !sc.isHasMarked();
		}
		return MAIN;
	}
	
	public void getpagedetail() {
		int totalNumber = 0;
		if(courseid != null)
			totalNumber = getSelectCourseService().markdCountByCourseId(courseid);
		setItemCount(totalNumber);
		int currentpage = getCurrentPage();
		int pagesize =8;
		setPageSize(pagesize);
		int start = (currentpage - 1) * pagesize;
		if(courseid != null){
			markedList = getSelectCourseService().getMarkedList(courseid,start, pagesize);
		}
	}
	
	
	
	public CourseBean getCourse() {
		if(courseid != null )
			course = getCourseService().getBeanById(courseid);
		return course;
	}
	public String getSelectedCourseId() {
		return selectedCourseId;
	}
	public void setSelectedCourseId(String selectedCourseId) {
		this.selectedCourseId = selectedCourseId;
	}

	public void setCourse(CourseBean course) {
		this.course = course;
	}

	public String getStarLevel() {
		return starLevel;
	}

	public void setStarLevel(String starLevel) {
		this.starLevel = starLevel;
	}

	public String getContents() {
		return contents;
	}

	public void setContents(String contents) {
		this.contents = contents;
	}

	public String getCourseid() {
		return courseid;
	}

	public void setCourseid(String courseid) {
		this.courseid = courseid;
	}
	
	public List<StudentMarkedHistoryBean> getMarkedList() {
		if(markedList == null)
			markedList = new ArrayList();
		return markedList;
	}

	public boolean isHasMarked() {
		return hasMarked;
	}
	
}
